using System.Net;
using System.Web.Script.Serialization;

namespace Salient.Web.ApplicationServices
{
    ///<summary>
    ///</summary>
    public static class AuthenticationServiceHelper
    {
        ///<summary>
        ///</summary>
        public const string EndpointName = "Authentication_JSON_AppService.axd";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootUrl"></param>
        /// <param name="cookies"></param>
        /// <returns></returns>
        public static bool FormsIsLoggedInAppSvc(string rootUrl, CookieContainer cookies)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/IsLoggedIn", null, cookies)
                .GetResponse().GetResponseStream().Text();

            return new JavaScriptSerializer().CleanAndDeserialize<bool>(response);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootUrl"></param>
        /// <param name="cookies"></param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <param name="createPersistentCookie"></param>
        /// <returns></returns>
        public static bool FormsLoginAppSvc(string rootUrl, CookieContainer cookies, string userName, string password,
                                            bool createPersistentCookie)
        {
            string response = RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/Login", new
                    {
                        userName,
                        password,
                        createPersistentCookie
                    }, cookies)
                .GetResponse().GetResponseStream().Text();

            return new JavaScriptSerializer().CleanAndDeserialize<bool>(response);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rootUrl"></param>
        /// <param name="cookies"></param>
        public static void FormsLogoutAppSvc(string rootUrl, CookieContainer cookies)
        {
            RequestBuilder
                .PostJsonApp(rootUrl + EndpointName + "/Logout", null, cookies)
                .GetResponse();
        }
    }
}